package com.emptyMe.org.service.imp;

import com.emptyMe.org.domain.Department;
import com.emptyMe.org.mapper.DepartmentMapper;
import com.emptyMe.org.query.DepartmentQuery;
import com.emptyMe.org.service.IDepartmentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper mapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void save(Department d) {
        mapper.save(d);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void remove(Long id) {
        mapper.remove(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void removeMany(List<Department> depts) {
        for (Department dept : depts) {
            mapper.remove(dept.getId());
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(Department d) {
        mapper.update(d);
    }

    @Override
    public Department getById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public List<Department> getAll() {
        return mapper.loadAll();
    }

    @Override
    public Long queryCount(DepartmentQuery query) {
        return mapper.queryCount(query);
    }

    @Override
    public PageInfo<Department> queryData(DepartmentQuery query) {
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        List<Department> departments = mapper.queryData(query);
        Page<Department> pages = (Page<Department>) departments;
        return pages.toPageInfo();
    }

    @Override
    public PageInfo<Department> searchData(DepartmentQuery query) {
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        List<Department> departments = mapper.searchData(query);
        Page<Department> pages = (Page<Department>) departments;
        return pages.toPageInfo();
    }
}
